#include "../logs/bitlog.h"
#include <unistd.h>

void test_log()
{
    bitlog::Logger::ptr logger = bitlog::LoggerManger::getInstance().getLogger("async_logger");
    logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger->info(__FILE__, __LINE__, "%s", "测试日志");
    logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    logger->error(__FILE__, __LINE__, "%s", "测试日志");
    logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
    size_t count = 0;
    while (count < 500000)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    }
}

void test_log_m()
{
    // bitlog::Logger::ptr logger = bitlog::LoggerManger::getInstance().getLogger("async_logger");
    // logger->debug("%s", "测试日志");
    // logger->info("%s", "测试日志");
    // logger->warn("%s", "测试日志"); 
    // logger->error("%s", "测试日志");
    // logger->fatal("%s", "测试日志");
    // size_t count = 0;
    // while (count < 500000)
    // {
    //     logger->fatal("测试日志-%d", count++);
    // }



    DEBUG("%s", "测试日志");
    INFO("%s", "测试日志");
    WARN("%s", "测试日志"); 
    ERROR("%s", "测试日志"); 
    FATAL("%s", "测试日志");
    size_t count = 0;
    while (count < 500000)
    {
        FATAL("测试日志-%d", count++);
    }
}

void test_log_a(const std::string& name)
{
    INFO("%s", "测试开始");
    bitlog::Logger::ptr logger = bitlog::LoggerManger::getInstance().getLogger(name);
    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn("%s", "测试日志"); 
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");
    INFO("%s", "测试完毕");
    
}

int main()
{
    // // 测试文件的路径
    // std::cout << bitlog::util::Date::now() << std::endl;
    // std::string pathname = "./abc/bcd/a.txt";
    // bitlog::util::File::createDirectory(bitlog::util::File::path(pathname));

    // // 测试日志等级
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::DEBUG) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::INFO) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::WARN) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::ERROR) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::FATAL) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::OFF) << std::endl;

    // // 测试format
    // bitlog::LogMsg msg(bitlog::LogLevel::value::INFO, 53, "main.c", "root", "格式化功能测试...");
    // bitlog::Formatter fmt("abc%%abc%g%g%g[%d{%H:%M:%S}] %m%n");
    // std::string str = fmt.format(msg);
    // std::cout << str << std::endl;


    // // 测试日志落地模块
    // bitlog::LogMsg msg(bitlog::LogLevel::value::INFO, 53, "main.c", "root", "格式化功能测试...");
    // bitlog::Formatter fmt;
    // std::string str = fmt.format(msg);
    // bitlog::LogSink::ptr stdout_lsp = bitlog::SinkFactory::create<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp = bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr roll_lsp = bitlog::SinkFactory::create<bitlog::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // stdout_lsp->log(str.c_str(), str.size());
    // file_lsp->log(str.c_str(), str.size());
    
    // size_t cursize = 0;
    // size_t count = 0;
    // while (cursize < 1024 * 1024 * 10)
    // {
    //     std::string tmp = str + std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(), tmp.size());
    //     cursize += tmp.size();
    // }

    // // 测试新增日志落地模块
    // bitlog::LogMsg msg(bitlog::LogLevel::value::INFO, 53, "main.c", "root", "格式化功能测试...");
    // bitlog::Formatter fmt;
    // std::string str = fmt.format(msg);
    // bitlog::LogSink::ptr time_lsp = bitlog::SinkFactory::create<RollByTimeSink>("./logfile/roll-", TimeGap::GAP_SECOND);
    
    // time_t old = bitlog::util::Date::now();
    // while (bitlog::util::Date::now() < old + 5)
    // {
    //     time_lsp->log(str.c_str(), str.size());
    //     usleep(1000);
    // }

    // // 测试日志器模块 -- 同步日志器
    // std::string logger_name = "Sync_logger";
    // bitlog::LogLevel::value limit = bitlog::LogLevel::value::WARN;
    // bitlog::Formatter::ptr fmt(new bitlog::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
    // bitlog::LogSink::ptr stdout_lsp = bitlog::SinkFactory::create<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp = bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr roll_lsp = bitlog::SinkFactory::create<bitlog::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // std::vector<bitlog::LogSink::ptr> sinks = {stdout_lsp, file_lsp, roll_lsp};
    // bitlog::Logger::ptr logger(new bitlog::SyncLogger(logger_name, limit, fmt, sinks));

    // // 测试建造者模式的日志器模块
    // std::unique_ptr<bitlog::LoggerBuilder> builder(new bitlog::LocalLoggerBuilder());
    // builder->buildLoggerName("sync_logger");
    // builder->buildLoggerLevel(bitlog::LogLevel::value::WARN);
    // builder->buildFormatter("%m%n");
    // builder->buildLoggerType(bitlog::LoggerType::LOGGER_SYNC);
    // builder->buildSink<bitlog::FileSink>("./logfile/test.log");
    // builder->buildSink<bitlog::StdoutSink>();
    // bitlog::Logger::ptr logger = builder->build();


    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
    // size_t cursize = 0, count = 0;
    // while (cursize < 1024 * 1024 * 10)
    // {
    //     logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    //     cursize += 20;
    // }


    // // 测试异步日志器的缓冲区
    // // 读取文件数据,一点一点写入缓冲区,最终将缓冲区数据写入文件,判断生成的新文件与源文件是否一致
    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if (ifs.is_open() == false) {std::cout << "open file\n"; return -1;}
    // ifs.seekg(0, std::ios::end); // 读取位置跳转到文件末尾
    // size_t fsize = ifs.tellg(); // 读取当前读写位置相对于起始位置的偏移量
    // ifs.seekg(0, std::ios::beg); // 重新跳转到起始位置
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);
    // if (ifs.good() == false)    {std::cout << "read error\n"; return -1;}
    // ifs.close();

    // std::cout << fsize << std::endl;
    // bitlog::Buffer buffer;
    // for (int i = 0; i < body.size(); i++)
    // {
    //     buffer.push(&body[i], 1);
    // }
    // std::cout << buffer.readAbleSize() << std::endl;

    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // size_t rsize = buffer.readAbleSize();
    // for (int i = 0; i < rsize; i++)
    // {
    //     ofs.write(buffer.begin(),  1);
    //     if (ofs.good() == false)    {std::cout << "write error\n"; return -1;}
    //     buffer.moveReader(1);
    // }

    //ofs.close();


    // // 异步日志器的测试
    // std::unique_ptr<bitlog::LoggerBuilder> builder(new bitlog::GobalLoggerBuilder());
    // builder->buildLoggerName("async_logger");
    // builder->buildLoggerLevel(bitlog::LogLevel::value::WARN);
    // builder->buildFormatter("[%c][%f:%l]%m%n");
    // builder->buildEnableUnSafeAsync();  // 测试性能
    // builder->buildLoggerType(bitlog::LoggerType::LOGGER_ASYNC);
    // builder->buildSink<bitlog::FileSink>("./logfile/async.log");
    // builder->buildSink<bitlog::StdoutSink>();
    // builder->build();

    // test_log_m();


    // 完整测试
    std::unique_ptr<bitlog::LoggerBuilder> builder(new bitlog::GobalLoggerBuilder());
    builder->buildLoggerName("sync_logger");
    builder->buildLoggerLevel(bitlog::LogLevel::value::DEBUG);
    builder->buildFormatter("[%c][%f:%l][%p]%m%n");
    builder->buildLoggerType(bitlog::LoggerType::LOGGER_SYNC);
    builder->buildSink<bitlog::FileSink>("./logfile/sync.log");
    builder->buildSink<bitlog::StdoutSink>();
    builder->buildSink<bitlog::RollBySizeSink>("./logfile/roll-sync-by-size", 1024 * 1024);
    builder->build();
    test_log_a("sync_logger");
    return 0; 
}
